﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Licenta1.Expressions
{
    [Serializable]
    class MathBinaryExpression : Expression
    {
        Expression expr1, expr2;
        string sign;

        public MathBinaryExpression(Context con, Expression e1, GenericExpression sign, Expression e2)
            : base(con)
        {
            expr1 = e1;
            expr2 = e2;
            this.sign = (string)sign.Value;
        }

        public override object Value
        {
            get
            {
                try
                {
                    double m1 = Convert.ToDouble(expr1.Value);
                    double m2 = Convert.ToDouble(expr2.Value);
                    switch (sign)
                    {
                        case "/": return (m1 / m2);
                        case "*": return (m1 * m2);
                        case "+": return (m1 + m2);
                        case "-": return (m1 - m2);
                        case "%": return (m1 % m2);
                        default: return 0;
                    }
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public Expression E1
        {
            get { return expr1; }
        }

        public Expression E2
        {
            get { return expr2; }
        }

        public string SIGN
        {
            get { return sign; }
        }
    }
}